package com.sinocare.system.controller;

import com.sinocare.common.entity.Menu;
import com.sinocare.common.entity.Role;
import com.sinocare.common.entity.RoleMenu;
import com.sinocare.common.result.Result;
import com.sinocare.common.result.ResultCode;
import com.sinocare.common.vo.RoleMenuVo;
import com.sinocare.system.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/role")
@Api(tags = "角色权限")
@CrossOrigin
public class RoleController {
    @Resource
    private RoleService roleService;

    @ApiOperation(value = "查询所有角色",httpMethod = "GET")
    @RequestMapping(value = "findAll",method = RequestMethod.GET)
    public Result queryRole(Role role){
        List<Role> list = roleService.query(role);
        System.out.println(list);
        return Result.ok(ResultCode.SUCCESS,list);
    }
    @ApiOperation(value = "增加角色",httpMethod = "POST")
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Result addRole(Role role){
        int i = roleService.add(role);
        if(i>0){
            return Result.ok(ResultCode.SUCCESS,i);
        }else {
            return Result.fail(ResultCode.FAIL, i);
        }
    }
    @ApiOperation(value = "修改角色信息",httpMethod = "GET")
    @RequestMapping(value = "update",method = RequestMethod.GET)
    public Result updateRole(Role role){
        int i = roleService.update(role);
        if (i>0){
            return Result.ok(ResultCode.SUCCESS,i);
        }else{
            return Result.fail(ResultCode.FAIL,i);
        }
    }
    @ApiOperation(value = "授权",httpMethod = "GET")
    @RequestMapping(value = "roleMenu",method = RequestMethod.GET)
    public Result role(Integer id){
        List<RoleMenuVo> arr=new ArrayList<>();
        //查询父
        List<Menu> menus = roleService.listRole();
        List<RoleMenu> roleMenus = roleService.listRoleMenu(id);
        for (Menu menu : menus) {
            RoleMenuVo roleMenuVo = new RoleMenuVo();
            roleMenuVo.setId(menu.getMenuId());
            roleMenuVo.setTitle(menu.getMenuName());
            roleMenuVo.setPId(menu.getPId());
            roleMenuVo.setUrl(menu.getUrl());
            roleMenuVo.setSeq(menu.getSeq());
            roleMenuVo.setStatus(menu.getStatus());
            roleMenuVo.setCreateTime(menu.getCreateTime());
            roleMenuVo.setUpdateTime(menu.getUpdateTime());
            roleMenuVo.setRemark(menu.getRemark());
            roleMenuVo.setType(menu.getType());
            for (RoleMenu roleMenu : roleMenus) {
               try{
                   if (roleMenu.getMenuId()==menu.getMenuId()){
                       roleMenuVo.setChecked(true);
                       break;
                   }else{
                       roleMenuVo.setChecked(false);
                   }
               }catch (Exception e){
                   roleMenuVo.setChecked(false);
               }
            }

            List<RoleMenuVo> list=new ArrayList<>();
            List<Menu> menus1 = roleService.listMenu(menu.getMenuId());
            for (Menu menu1 : menus1) {
                RoleMenuVo roleMenu1=new RoleMenuVo();
                roleMenu1.setId(menu1.getMenuId());
                roleMenu1.setTitle(menu1.getMenuName());
                roleMenu1.setPId(menu1.getPId());
                roleMenu1.setUrl(menu1.getUrl());
                roleMenu1.setSeq(menu1.getSeq());
                roleMenu1.setStatus(menu1.getStatus());
                roleMenu1.setCreateTime(menu1.getCreateTime());
                roleMenu1.setUpdateTime(menu1.getUpdateTime());
                roleMenu1.setRemark(menu1.getRemark());
                roleMenu1.setType(menu1.getType());
                for (RoleMenu roleMenu : roleMenus) {
                    try{
                        if (roleMenu.getMenuId()==menu1.getMenuId()){
                            roleMenu1.setChecked(true);
                            break;
                        }else{
                            roleMenu1.setChecked(false);
                        }
                    }catch (Exception e){
                        roleMenu1.setChecked(false);
                    }
                }
                list.add(roleMenu1);
            }
           roleMenuVo.setChildren(list);
            arr.add(roleMenuVo);
        }
        return Result.ok(ResultCode.SUCCESS,arr);
    }

    @ApiOperation(value = "授权",httpMethod = "POST")
    @RequestMapping(value = "add1",method = RequestMethod.POST)
    public Result aa(Integer id,Integer [] list){
        int i = roleService.deleteRole(id);
        List<Map<String,Integer>> s=new ArrayList<>();
        for (int i1 = 0; i1 < list.length; i1++) {
            Map<String,Integer> map = new HashMap<>();
            map.put("id",id);
            map.put("ids",list[i1]);
            s.add(map);
        }
            roleService.addRole(s);



        return Result.ok(ResultCode.SUCCESS,i);

    }


}
